﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bike.Repository;

namespace Bike.Biz
{
    public class OrderService : BikeService<Order>
    {
        public void Imports(Order order, Transaction t)
        {
            order.Note = "Nhập hàng.";
            foreach (var item in order.OrderDetails)
	        {
                item.Total = item.Quantity * item.Costs;
	        }
            var context = ContextManager.GetInstance();
            var contextAdapter = new ObjectContextAdapter(context);
            var unitOfWork = new UnitOfWork(contextAdapter);
            var repository = new OrderRepository(contextAdapter);

            try
            {
                repository.Add(order);
                if (t.Amount > 0)
                {
                    var tRepository = new TransactionRepository(contextAdapter);
                    tRepository.Add(t);
                }
                unitOfWork.Commit();
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
    }

}
